package com.iraeta.service.basic.impl;

import com.iraeta.dao.main.PageDao;
import com.iraeta.dao.main.PageuserDao;
import com.iraeta.entity.main.Page;
import com.iraeta.entity.main.Pageuser;
import com.iraeta.entity.main.User;
import com.iraeta.service.basic.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    PageDao pageDao;
    @Autowired
    PageuserDao pageuserDao;

    @Override
    public List<Page> listPage(String pageName) {
        return pageDao.selectByPageName(pageName);
    }

    @Override
    public List<User> listPageUser(Integer pageId) {
        return pageuserDao.listPageUser(pageId);
    }

    @Override
    public Pageuser getPageuser(Integer pageId, Integer uid) {
        return pageuserDao.selectByPageIdAndUId(pageId, uid);
    }

    @Transactional
    @Override
    public void savePageUser(Integer pageId, List<Integer> users) {
        pageuserDao.deleteByPageId(pageId);
        for (Integer userId : users) {
            Pageuser pageuser = new Pageuser(pageId, userId);
            pageuserDao.insert(pageuser);
        }
    }

    @Override
    public List<Page> listUserPage(Integer userId) {
        return pageuserDao.listUserPage(userId);
    }
}
